package com.atguigu.gmall.common.interceptors;

import com.atguigu.gmall.common.config.utils.UserAuthUtil;
import com.atguigu.gmall.common.constant.RedisConst;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description 透传用户头信息；将老请求请求头中的东西放到新请求头中
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-16 20:46
 **/
@Slf4j
public class UserAuthFeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        log.info("feign拦截器--------------------------------");
        //1、获取老请求
        HttpServletRequest request = UserAuthUtil.request();
        String userId = request.getHeader(RedisConst.USER_ID_HEADER);
        if (!StringUtils.isEmpty(userId)) {
            //userId传下去
            template.header(RedisConst.USER_ID_HEADER, userId);
        }
        String tempId = request.getHeader(RedisConst.USER_TEMP_ID_HEADER);
        if (!StringUtils.isEmpty(tempId)) {
            //tempId传下去
            template.header(RedisConst.USER_TEMP_ID_HEADER, tempId);
        }
    }
}
